package com.leyou.item.comsume;

import com.leyou.item.service.SpecGroupService;
import com.leyou.item.service.SpecParamService;
import com.leyou.pojo.dto.GroupDTO;
import com.leyou.pojo.dto.ParamDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/spec")
public class SpecComsume {


    @Autowired
    SpecGroupService specGroupService;
    @Autowired
    SpecParamService specParamService;

    /**
     * 新增规格组
     * @param groupDTO
     * @return
     */
    @PostMapping("/group")
    public ResponseEntity<Void> addSpecGroup(@RequestBody GroupDTO groupDTO){
        specGroupService.addOrUpdateSpecGroup(groupDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 修改规格组
     * @param groupDTO
     * @return
     */
    @PutMapping("/group")
    public ResponseEntity<Void> updateSpecGroup(@RequestBody GroupDTO groupDTO){
        specGroupService.addOrUpdateSpecGroup(groupDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 根据商品分类id，查询规格组的集合
     * @param cid
     * @return
     */
    @GetMapping("/groups/of/category")
    public ResponseEntity<List<GroupDTO>> findGroudsByCategoryId(@RequestParam("id") Long cid) {
        return ResponseEntity.ok(specGroupService.findGroudsByCategoryId(cid));
    }

    /**
     * 根据分类id查询规格组及组内参数
     * @param cid
     * @return
     */
    @GetMapping("/list")
    public ResponseEntity<List<GroupDTO>> findGroudsAndParamsByCategoryId(@RequestParam("id") Long cid) {
        return ResponseEntity.ok(specGroupService.findGroudsAndParamsByCategoryId(cid));
    }


    // -------------- param ----------------


    /**
     * 新增规格参数
     * @param paramDTO
     * @return
     */
    @PostMapping("/param")
    public ResponseEntity<Void> addSpecParam(@RequestBody ParamDTO paramDTO) {
        specParamService.addOrUpdateSpecParam(paramDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 修改规格参数
     * @param paramDTO
     * @return
     */
    @PutMapping("/param")
    public ResponseEntity<Void> updateSpecParam(@RequestBody ParamDTO paramDTO) {
        specParamService.addOrUpdateSpecParam(paramDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 根据条件查询规格参数集合
     * @param categoryId 商品分类id
     * @param groupId 规格组id
     * @param searching 是否搜索
     * @return
     */
    @GetMapping("/params")
    public ResponseEntity<List<ParamDTO>> findParamsByIdOrSearching(
            @RequestParam(value = "categoryId", required = false) Long categoryId,
            @RequestParam(value = "groupId", required = false) Long groupId,
            @RequestParam(value = "searching", required = false) Boolean searching
    ) {
        return ResponseEntity.ok(specParamService.findParamsByIdOrSearching(categoryId,groupId,searching));
    }
}
